//

// (  )
//

package com.tools.acceleratecore.xlib.xlib.impl;

import com.dianxinos.acceleratecore.xlib.xlib.intf.IXObject;
import com.tools.acceleratecore.xlib.xlib.intf.IXFactory;
import com.tools.acceleratecore.xlib.xlib.intf.IXManager;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class XFactoryAutoRemove implements IXFactory {
    protected Map<Class<?>, XFactoryAutoRemove.XFactoryImplementMap> xFactoryInterfaceMap = null;

    public XFactoryAutoRemove() {
    }

    public IXObject createInstance(Class<?> classInterface) {
        return this.createInstance(classInterface, (Class) null);
    }

    public IXObject createInstance(Class<?> classInterface, Class<?> classImplement) {
        if (classInterface != null && IXObject.class.isAssignableFrom(classInterface)) {
            XFactoryAutoRemove.XFactoryImplementMap xFactoryImplementMap = this.findImplementMap(classInterface);
            if (xFactoryImplementMap == null) {
                return null;
            } else {
                int nImplementPos = this.findImplementPos(xFactoryImplementMap, classImplement);
                if (-1 == nImplementPos) {
                    return null;
                } else {
                    IXObject iXObject = null;
                    Class var6;
                    if (IXManager.class.isAssignableFrom(classInterface)) {
                        var6 = XFactory.class;
                        synchronized (XFactory.class) {
                            if (((WeakReference) xFactoryImplementMap.mListIXObject.get(nImplementPos)).get() != null) {
                                iXObject = (IXObject) ((WeakReference) xFactoryImplementMap.mListIXObject.get(nImplementPos)).get();
                            } else {
                                try {
                                    iXObject = (IXObject) xFactoryImplementMap.mArrayClassImplement[nImplementPos].newInstance();
                                    WeakReference<IXObject> wr = new WeakReference(iXObject);
                                    xFactoryImplementMap.mListIXObject.set(nImplementPos, wr);
                                } catch (Exception var10) {
                                    var10.printStackTrace();
                                }
                            }
                        }
                    } else {
                        var6 = XFactory.class;
                        synchronized (XFactory.class) {
                            try {
                                iXObject = (IXObject) xFactoryImplementMap.mArrayClassImplement[nImplementPos].newInstance();
                            } catch (Exception var8) {
                                var8.printStackTrace();
                            }
                        }
                    }

                    return iXObject;
                }
            }
        } else {
            return null;
        }
    }

    private XFactoryAutoRemove.XFactoryImplementMap findImplementMap(Class<?> classInterface) {
        return classInterface == null ? null : (XFactoryAutoRemove.XFactoryImplementMap) this.xFactoryInterfaceMap.get(classInterface);
    }

    private int findImplementPos(XFactoryAutoRemove.XFactoryImplementMap xFactoryImplementMap, Class<?> classImplement) {
        if (xFactoryImplementMap != null && xFactoryImplementMap.mArrayClassImplement != null && xFactoryImplementMap.mArrayClassImplement.length != 0) {
            if (classImplement == null) {
                return 0;
            } else {
                for (int nIndex = 0; nIndex < xFactoryImplementMap.mArrayClassImplement.length; ++nIndex) {
                    if (classImplement == xFactoryImplementMap.mArrayClassImplement[nIndex].getClass()) {
                        return nIndex;
                    }
                }
                return -1;
            }
        } else {
            return -1;
        }
    }

    protected class XFactoryImplementMap {
        public Class[] mArrayClassImplement = null;
        public List<WeakReference<IXObject>> mListIXObject = null;

        public XFactoryImplementMap(Class<?>[] arrayIXObject, Class[] arrayClassImplement) {
            this.mArrayClassImplement = arrayClassImplement;
            if (arrayIXObject != null) {
                this.mListIXObject = new ArrayList();

                for (int nIndex = 0; nIndex < arrayIXObject.length; ++nIndex) {
                    WeakReference<IXObject> wr = new WeakReference(arrayIXObject[nIndex]);
                    this.mListIXObject.add(wr);
                }
            }

        }
    }
}
